package com.restaurant.application.util.counter.support;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;

public class CounterValue implements PickableBean {

    public static final String FN_INDEX = "index";
    public static final String FN_TIME = "time";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_INDEX,
            FN_TIME);

    private Integer index;
    private Long time;

    public CounterValue(Integer index, Long time) {
        setIndex(index);
        setTime(time);
    }

    public CounterValue() {
    }

    public CounterValue duplicate() {
        CounterValue result = new CounterValue();
        result.index = this.index;
        result.time = this.time;
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer value) {
        this.index = value;
    }

    public CounterValue withIndex(Integer value) {
        setIndex(value);
        return this;
    }

    public CounterValue withIndex(Integer value, Integer defaultValue) {
        setIndex(value == null ? defaultValue : value);
        return this;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long value) {
        this.time = value;
    }

    public CounterValue withTime(Long value) {
        setTime(value);
        return this;
    }

    public CounterValue withTime(Long value, Long defaultValue) {
        setTime(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "index":
                return getIndex();
            case "time":
                return getTime();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof CounterValue)) return false;
        return signature().equals(((CounterValue) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean CounterValue has no signature defined, no hash code or equals possible.");
    }
}
